package com.ewo.controller;

import com.ewo.entity.Emp;
import com.ewo.entity.EmpInfo;
import com.ewo.entity.ResponseResult;
import com.ewo.entity.workOrder.WorkOrderInfo;
import com.ewo.service.EmpService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/emp")
@CrossOrigin("*")
public class EmpController {

    @Resource
    EmpService empService;

    //员工登录页面
    @PostMapping("/login")
    public ResponseResult login(String username, String password) {
        // 从SecurityUtils里边创建一个 subject
        Subject subject = SecurityUtils.getSubject();
        //提交token
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        //认证登录
        try {
            subject.login(token);
        } catch (AccountException accountException) {
            return ResponseResult.failCode(accountException.getMessage());
        } catch (IncorrectCredentialsException incorrectCredentialsException) {
            return ResponseResult.failCode("密码不正确");
        } catch (AuthenticationException authenticationException) {
            return ResponseResult.failCode("用户名或密码不正确");
        }
        if (subject.isAuthenticated()) {
            //登录成功,修改最后登录时间
            LocalDateTime loginTime = LocalDateTime.now();
            empService.updateEmpByUsername(username, loginTime);
            //获取当前登录员工信息
            Emp emp = empService.findEmpByUsername(username);
            if (Objects.isNull(emp)) {
                return ResponseResult.fail();
            }
            List<Object> list = new ArrayList<>();
            list.add(emp);
            list.add(token);
            return ResponseResult.ok(list);
        }
        return ResponseResult.failCode("登录失败");
    }

    //查找所有员工信息
    //@GetMapping("/listEmp")
    public ResponseResult findAllEmp() {
        List<Emp> empList = empService.findAllEmp();
        if (empList != null && empList.size() != 0) {
            return ResponseResult.ok(empList);
        }
        return ResponseResult.fail();
    }

    //查询单个员工信息
    @PostMapping("/findEmp")
    public ResponseResult findEmpByUsername(String username) {
        Emp emp = empService.findEmpByUsername(username);
        if (Objects.isNull(emp)) {
            return ResponseResult.fail();
        }
        return ResponseResult.ok(emp);
    }

    //修改员工信息
    @PostMapping(value = "/updateEmp")
    public ResponseResult updateEmpById(@ModelAttribute("emp") Emp emp) {
        System.out.println("打印时间:" + emp.getBirth_date());
        int rows = empService.updateEmpById(emp);
        return rows == 0 ? ResponseResult.fail() : ResponseResult.ok(null);
    }

    //修改员工密码
    @PostMapping("/updateEmpPwd")
    public ResponseResult updateEmpPwdByUsername(@RequestParam("username") String username,
                                                 @RequestParam("oldPassword") String oldPassword,
                                                 @RequestParam("newPassword") String newPassword) {
        //根据oldPassword查询密码
        String password = empService.FindPwdByUsername(username);
        if (oldPassword.equals(password)) {
            //根据员工username修改password
            int rows = empService.UpdatePwdByUsername(username, newPassword);
            return rows == 0 ? ResponseResult.fail() : ResponseResult.ok(null);
        }
        return ResponseResult.failCode("原密码输入错误");
    }

    //添加员工
    //@PostMapping("/addEmp")
    //public ResponseResult addEmp(@RequestBody Emp emp) {
    //Emp emp = new Emp(0,"张三", "13133615584", "123456", "123456789", "13132145121",
    //        "猪猪侠", "zhuzhuxia@qq.com", LocalDateTime.now(), 1, "陕西西安",
    //        "大学本科", "电子信息", "野猪大学", LocalDateTime.now(), "降龙十巴掌创始人", 2);
    //    emp.setGraduation_time(LocalDateTime.now());
    //    int rows = empService.addEmp(emp);
    //    return rows == 0?ResponseResult.fail():ResponseResult.ok(null);
    //}

    //员工显示页面
    @GetMapping("/empInfo/{empId}")
    public ResponseResult empInfo(@PathVariable("empId") int empId) {
        //查询待服务2工单
        int dfw = empService.findEmpInfoByUsername(empId, 2);
        //查询服务中3工单
        int fwz = empService.findEmpInfoByUsername(empId, 3);
        //查询已关闭4工单
        int finish = empService.findEmpInfoByUsername(empId, 4);
        int sum = dfw + fwz + finish;
        EmpInfo empInfo = new EmpInfo(dfw, fwz, finish, sum);
        if (Objects.isNull(empInfo)) {
            return ResponseResult.fail();
        }
        return ResponseResult.ok(empInfo);
    }

    //查询当前员工各个状态的工单
    @GetMapping("/empOrderStatus/{empId}/{work_status}")
    public ResponseResult empInfo(@PathVariable("empId") int empId, @PathVariable("work_status") int work_status) {
        List<WorkOrderInfo> workOrderInfoList = empService.findEmpOrderByEmpIdAndWork_status(empId, work_status);
        if (workOrderInfoList == null || workOrderInfoList.size() == 0) {
            return ResponseResult.fail();
        }
        return ResponseResult.ok(workOrderInfoList);
    }

    //退出登录
    @RequestMapping("/logout")
    public ResponseResult logout() {
        System.out.println("退出登录");
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return ResponseResult.okCode("退出登录成功");
    }


}
